package cn.com.wxd.biz.log;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.wxd.dao.log.AccessLogDao;
import cn.com.wxd.entity.log.AccesslogInfo;
import cn.com.wxd.entity.log.AccesslogTempInfo;
import cn.com.wxd.entity.log.UserLogInfo;
import cn.com.wxd.entity.user.AccountInfo;
import cn.com.wxd.common.DateUtil;
import cn.com.wxd.common.PageInfo;
import cn.com.wxd.common.data.KVMap;

/**
 * Title:AccessLogServiceImpl.java
 * Description:cn.com.wxd.biz.log
 * <p>
 * Company: Amumu管理平台
 * Copyright: Copyright (c) 2015
 * All right reserved.
 * Created time: 2016-4-14 下午4:45:53
 *
 * @author WangXuDong
 * @version 1.0
 */
public class AccessLogServiceImpl implements AccessLogService {
    private static final Logger log = LoggerFactory.getLogger(AccessLogServiceImpl.class);
    private AccessLogDao accessLogDao;

    @Override
    public int getVisitorVolume() {
        return accessLogDao.getVisitorVolume();
    }

    @Override
    public int getPvbyAddress(String country, String area, String city) {
        return accessLogDao.getPvbyAddress(country, area, city);
    }

    @Override
    public boolean archiveAccessRecode(List<AccesslogInfo> ai) {
        try {
            accessLogDao.archiveAccessRecode(ai);
            accessLogDao.cleanAccesslogTempTable();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public int getPVofToday() {
        return accessLogDao.getPVofToday();
    }

    @Override
    public boolean addAccessRecode(AccesslogTempInfo ati) {
        return accessLogDao.addAccessRecode(ati);
    }

    @Override
    public int getAccessRecodeCountByIp(String ip) {
        return accessLogDao.getAccessRecodeCountByIp(ip);
    }

    @Override
    public List<AccesslogTempInfo> selectAccesslogFormTemp() {
        return accessLogDao.selectAccesslogFormTemp();
    }

    @Override
    /**
     * 添加一条用户日志
     */
    public boolean addUserLog(AccountInfo accountInfo, String operate) {
        try {
            UserLogInfo userlog = new UserLogInfo();
            userlog.setUid(accountInfo.getUsersInfo().getId());
            userlog.setTime(DateUtil.getDatetimeStr());
            userlog.setOperate(operate);
            userlog.setUserName(accountInfo.getUserName());
            boolean result = accessLogDao.insertUserLog(userlog);
            return result;
        } catch (Exception e) {
            log.error("加入用户:" + accountInfo.getUsersInfo().getId() + "日志:" + operate + "失败！" + e.getMessage(), e);
            return false;
        }

    }

    @Override
    public List<UserLogInfo> getUserLog(int userId, PageInfo page) {
        if (userId == 0) {
            return null;
        }
        return accessLogDao.selectUserLog(userId, page);
    }

    @Override
    public List<UserLogInfo> getUserLog(KVMap<String, Object> param,
                                        KVMap<String, String> order, List<String> fuzzField, PageInfo page) {
        return accessLogDao.selectUserLog(param, order, fuzzField, page);
    }

    @Override
    public List<UserLogInfo> getUserLogInTimeBuc(int userId, String begintime,
                                                 String endtime, PageInfo page) {
        return accessLogDao.selectUserLogInTimeBuc(userId, begintime, endtime, page);
    }

    @Override
    public int[] queryGroupByDay(int dayNum) {
        int[] retints = accessLogDao.selectGroupByDay(dayNum);
        if (retints != null) {
            //将数组的排序翻转
            int[] retints2 = new int[retints.length];
            int j = 0;
            for (int i = retints.length - 1; i >= 0; i--) {
                retints2[j] = retints[i];
                j++;
            }
            return retints2;
        }
        return null;
    }

    @Override
    public int queryVVByDay(String dateTime) {
        return accessLogDao.selectVVByDay(dateTime);
    }

    public AccessLogDao getAccessLogDao() {
        return accessLogDao;
    }

    public void setAccessLogDao(AccessLogDao accessLogDao) {
        this.accessLogDao = accessLogDao;
    }
}
